package com.briup.estore.web.controller;

import com.briup.estore.constant.BookStoreConstant;
import com.briup.estore.dto.UserLoginDTO;
import com.briup.estore.dto.UserRegisterDTO;
import com.briup.estore.service.UserService;
import com.briup.estore.vo.UserInfoVO;
import com.briup.estore.vo.UserLoginVO;
import com.briup.estore.web.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "用户模块")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody UserLoginDTO userLoginDTO){
        UserLoginVO userLoginVO = userService.login(userLoginDTO, BookStoreConstant.LOGIN_USER);
        return Result.success(userLoginVO);
    }

    @GetMapping("/getUserInfo")
    @ApiOperation("获取用户详细信息")
    public Result getUserInfo(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        UserInfoVO vo = userService.getUserInfo(userId);
        return Result.success(vo);
    }


    @PostMapping("/register")
    @ApiOperation("新用户注册")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO){
        UserRegisterDTO userRegisterDTO1;
        userRegisterDTO1 = userRegisterDTO;
        userService.register(userRegisterDTO1);
        return Result.success();
    }
}
